package map;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

/**
 * @author feng
 * @Date 2022/2/25 21:34
 * 使用Map统计集合出现的次数
 */
public class MapTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入字符串: ");
        String s = sc.nextLine();
        toMap(s);
    }

    public static void toMap(String str) {
        HashMap<Character, Integer> map = new HashMap<>();
        for (char c : str.toCharArray()) {
            if (map.containsKey(c)) {
                Integer value = map.get(c);
                map.put(c, value + 1);
            } else {
                //key不存在
                map.put(c, 1);
            }

        }
        Set<Map.Entry<Character, Integer>> entries = map.entrySet();
        for (Map.Entry entry : entries) {
            System.out.println("key="+entry.getKey()+" 次数="+entry.getValue());
        }
    }
}
